﻿using B.S.BasicData.ErrorCode;
using B.S.BasicData.Write.Application.Common.User;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.BasicData.Write.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class UserController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<UserController> logger;

        public UserController(IMediator mediator,ILogger<UserController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<int>> CreateUserHandle(CreateUserCommon request)
        {
            try
            {
                logger.LogInformation("新增用户成功");
                return mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("新增用户异常"+ex.Message);
                throw;
            }
           
        }
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public Task<ApiResult<int>> UpdateUserHandle(UpdateUserCommon request)
        {
            try
            {
                logger.LogInformation("修改用户信息成功");
                return mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("修改用户信息失败"+ex.Message);
                throw;
            }
          
        }
        /// <summary>
        /// 逻辑删除用户信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public Task<ApiResult<int>> DelUserHandle(DelUserCommon request)
        {
            try
            {
                logger.LogInformation("逻辑删除用户信息成功");
                return mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("逻辑删除用户异常", ex.Message);
                throw;
            }
          
        }



        }
}
